package models;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.*;

import play.db.ebean.*;
import play.db.ebean.Model.Finder;
import play.data.format.*;
import play.data.validation.*;

import com.avaje.ebean.*;

@Entity
public class User extends Model {

	@Id
	private Long idUser;
	
	@Enumerated(EnumType.STRING)
	private Statut statut;
	private String numetudiant;

	@Constraints.Required
	private String login;

	@Constraints.Required
	private String password;
	private String nom;
	private String prenom;

	@Constraints.Email
	private String email;

	private List<User_Qcm> listeQcmPasses = new ArrayList<User_Qcm>();
	private List<Qcm> listeQcmCrees = new ArrayList<Qcm>();

	public static Finder<Long, User> find = new Finder<Long, User>(Long.class,
			User.class);

	/**
	 * La méthode permet de retrouver une question à partir de son ID
	 * 
	 * @param id
	 * @return Qcm
	 */
	public static User findById(Long id) {
		return Ebean.find(User.class, id);
	}

	public static User authenticate(String log, String pwd) {
		return find.where().eq("login", log).eq("password", pwd).findUnique();
	}

	public static List<User> findAll() {
		return find.all();
	}

	public static void create(User admin) {
		admin.save();
	}

	public static void update(User admin) {
		admin.update();
	}

	public static void delete(Long id) {
		find.ref(id).delete();
	}

	/*
	 * GETTER ET SETTER
	 */
	
	public Long getIdUser() {
		return idUser;
	}

	public void setIdUser(Long idUser) {
		this.idUser = idUser;
	}

	public Statut getStatut() {
		return statut;
	}

	public void setStatut(Statut statut) {
		this.statut = statut;
	}

	public String getNumetudiant() {
		return numetudiant;
	}

	public void setNumetudiant(String numetudiant) {
		this.numetudiant = numetudiant;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public String getPrenom() {
		return prenom;
	}

	public void setPrenom(String prenom) {
		this.prenom = prenom;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@OneToMany(mappedBy = "user")
	public List<User_Qcm> getListeQcmPasses() {
		return listeQcmPasses;
	}

	public void setListeQcmPasses(List<User_Qcm> listeQcmPasses) {
		this.listeQcmPasses = listeQcmPasses;
	}

	@OneToMany(mappedBy = "user")
	public List<Qcm> getListeQcmCrees() {
		return listeQcmCrees;
	}

	public void setListeQcmCrees(List<Qcm> listeQcmCrees) {
		this.listeQcmCrees = listeQcmCrees;
	}

}
